var dateUtils = require('../../utils/dateUtil.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // startYear: 2000,
    // endYear: 2050,
    chooseTime1:'2022-05-11',
    chooseTime2:'2022-06-03',
    chooseTimes1:0,
    chooseTimes2:0,
    dataList:[],
    totalPage:0,
    pageNo:1,
    pageSize:10,
    isLoading:false,
    studentId:'',
    userInfo:{}
  },
  bindDateChange1: function(e) {

    let repTime = e.detail.value.replace(/-/g, '/');
    let timeTamp = Date.parse(repTime); 
    console.log(timeTamp+' picker发送选择改变，携带值为', e.detail.value)
    if(timeTamp <= this.data.chooseTimes2){
      this.setData({
        chooseTimes1:timeTamp,
        chooseTime1: e.detail.value,
        pageNo:1,
        dataList:[],
        totalPage:0
      })
      this.getListData()
    }else{
      wx.showToast({
        title: '开始时间需小于结束时间',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    }
   
  },
  bindDateChange2: function(e) {
    
    let repTime = e.detail.value.replace(/-/g, '/');
    let timeTamp = Date.parse(repTime); 
    console.log(timeTamp+' picker发送选择改变，携带值为', e.detail.value)
    if(timeTamp>=this.data.chooseTimes1){
      this.setData({
        chooseTimes2:timeTamp,
        chooseTime2: e.detail.value,
        pageNo:1,
        dataList:[],
        totalPage:0
      })
      this.getListData()
    }else{
      wx.showToast({
        title: '结束时间需大于开始时间',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    }
   
  },


  // toInfo:function(ret){
  //   // 
  //   // console.log("11===="+ret.currentTarget.dataset)
  //   // console.log("11===="+JSON.stringify(ret.currentTarget.dataset))
  //   // console.log("===="+wx.getStorageSync('ifTeacher'))
  //   if(wx.getStorageSync('ifTeacher')){
  //     wx.navigateTo({
  //       url: '/pages/KetangjiluBanjiInfo/KetangjiluBanjiInfo',
  //     })
  //   }else{

  //     wx.navigateTo({
  //       url: '/pages/KetangjiluInfo/KetangjiluInfo?courseId=&studentId=',
  //     })
  //   }
  // },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var starTiem = dateUtils.mathChangeDate(dateUtils.formatTime(new Date()),"-",7,true,'')
    var endTiem = dateUtils.formatTime(new Date())

   //时间日期格式
    var endformtTiem = new Date(dateUtils.formatDataTime(new Date())).getTime()
    var starformtTiem = new Date(dateUtils.mathChangeDate(dateUtils.formatDataTime(new Date()),"-",7,false)).getTime()

    this.setData({
      chooseTime1:starTiem,
      chooseTime2:endTiem,
      chooseTimes1:starformtTiem,
      chooseTimes2:endformtTiem,
   })
      this.setData({
        studentId:wx.getStorageSync('userData').list[wx.getStorageSync('stuIndex')].studentId
      })
      console.log("stuid = "+this.data.studentId)
      this.getListData()
  },



  getListData(cd){
    wx.showLoading({
      title: '数据加载中...',
      icon: 'none',
    })
    this.setData({
      isLoading:true
    })
    var data = {
      beginTime:this.data.chooseTimes1,
      endTime:this.data.chooseTimes2,//"1653177295000"
      pageNo:this.data.pageNo,
      pageSize:this.data.pageSize,
      studentId:this.data.studentId
    }
    console.log(this.data.chooseTimes1+"=="+this.data.chooseTimes2+"=="+this.data.studentId);
    app.api.getStudentCourse(data).then(rest => {
      wx.hideLoading()
      this.setData({
        isLoading:false
      })
      cd && cd()
      if(rest&&rest.success){   
        if(rest.data.list!=null && rest.data.list.length > 0){
           this.setData({
            ifShowUI:false
           })
        }else{
          this.setData({
            ifShowUI:true
           })
        }
        this.setData({
          dataList:[...this.data.dataList,...rest.data.list],
          totalPage:rest.data.totalCount
        })
        console.log('接口数据='+JSON.stringify(rest))
      }else{
        console.log('接口数据=='+rest.errMsg)
        wx.showToast({
          title: rest.errMsg||'网络异常，请稍后重试！！',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
    })
    .catch(r => {
      console.error(r)
      wx.showToast({
        title: '网络异常，请稍后重试！',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    })
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },
 /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      pageNo:1,
      dataList:[],
      totalPage:0
    })
    this.getListData(()=>{
      wx.stopPullDownRefresh()
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log('触地时间')
    if(this.data.pageSize * this.data.pageNo >= this.data.totalPage){
      return
    }

    if(this.data.isLoading){
      return
    }

    this.setData({
      pageNo:this.data.pageNo + 1
    })
    this.getListData()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})